package com.sgcc.lms.openstack.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sgcc.lms.openstack.enumeration.StatusCode;
import com.sgcc.lms.openstack.exception.NullTokenException;
import org.openstack4j.api.OSClient;
import org.openstack4j.model.identity.v3.Token;
import org.openstack4j.openstack.OSFactory;
import org.openstack4j.openstack.identity.v3.domain.KeystoneToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-21 14:32
 * @Description:
 */
@Component
public class OpenStackClientFactory {


    @Autowired
    private  RedisTemplate<String, Object> redisTemplate;

    private  static  RedisTemplate<String, Object> redisTemplate1;

    private static final HashMap<String, Token> TOKEN_HASH_MAP = new HashMap<>();


    @PostConstruct
    public void init() {
        OpenStackClientFactory.redisTemplate1 =redisTemplate;
    }

    /**
     * @Author 李孟帅
     * @Description 获取OpenStack客户端
     * @Date 2020/7/18
     **/
    public static OSClient.OSClientV3 getOpenStackClient(String tokenId) throws NullTokenException {
        //根据tokenId从缓存从获取token
        Token token = (Token) redisTemplate1.opsForValue().get(tokenId);
        //获取客户端
        OSClient.OSClientV3 client = null;
        try {
            client = OSFactory.clientFromToken(token);
        } catch (Exception e) {
            throw new NullTokenException("从缓存中获取token为空," + e.getMessage(),StatusCode.UNAUTHORIZED.getCode());
        }
        return client;
    }

    /**
     * @Author 李孟帅
     * @Description 缓存token
     * @Date 2020/7/21
     **/
    public static void putToken(Token token)   {
        String tokenId = token.getId();
        Date expires = token.getExpires();
        redisTemplate1.opsForValue().set(tokenId,token);
        redisTemplate1.expireAt(tokenId, expires);
    }

    /**
     * @Author 李孟帅
     * @Description 获取当前项目的id
     * @Date 2020/7/22
     **/
    public static String getProjectId(String tokenId) throws NullTokenException {
        //根据tokenId从缓存从获取token
        Token token = (Token) redisTemplate1.opsForValue().get(tokenId);
        String id = null;
        try {
            id = token.getProject().getId();
        } catch (Exception e) {
            throw new NullTokenException("从缓存中获取token为空,或从token获取项目为空" + e.getMessage(), StatusCode.UNAUTHORIZED.getCode());
        }
        return id;
    }

}
